package org.dromara.creditcard.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.creditcard.domain.CreditBillTemplate;
import org.dromara.creditcard.domain.CreditPackage;
import org.dromara.creditcard.service.ICreditBillTemplateService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 账单拆分模板
 *
 * @author Mr.Zhou
 * @date 2023-11-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/credit/billTemplate")
public class CreditBillTemplateController extends BaseController {

    private final ICreditBillTemplateService creditBillTemplateService;

    /**
     * 查询账单拆分模板列表
     */
    @SaCheckPermission("credit:billTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<CreditBillTemplate> list(CreditBillTemplate creditBillTemplate, PageQuery pageQuery) {
        QueryWrapper<CreditBillTemplate> qw = new QueryWrapper<>();

        TableDataInfo<CreditBillTemplate> td = PageUtils.buildDataInfo(creditBillTemplateService.page(PageUtils.buildPage(),qw));
        List<CreditBillTemplate> list = td.getRows();
        return td;
    }

    /**
     * 导出账单拆分模板列表
     */
    @SaCheckPermission("credit:billTemplate:export")
    @Log(title = "账单拆分模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CreditBillTemplate creditBillTemplate, HttpServletResponse response) {
        List<CreditBillTemplate> list = creditBillTemplateService.list();
        ExcelUtil.exportExcel(list, "账单拆分模板", CreditBillTemplate.class, response);
    }

    /**
     * 养卡套餐列表(不分页，下拉框使用)
     */
    @SaCheckPermission("credit:billTemplate:list")
    @GetMapping("/noPage/list")
    public R<List<CreditBillTemplate>> noPageList(CreditBillTemplate creditBillTemplate) {
        List<CreditBillTemplate> list = creditBillTemplateService.list();
        return R.ok(list);
    }

    /**
     * 获取账单拆分模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("credit:billTemplate:query")
    @GetMapping("/{id}")
    public R<CreditBillTemplate> getInfo(@NotNull(message = "主键不能为空")  @PathVariable Long id) {
        return R.ok(creditBillTemplateService.getById(id));
    }

    /**
     * 新增账单拆分模板
     */
    @SaCheckPermission("credit:billTemplate:add")
    @Log(title = "账单拆分模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CreditBillTemplate creditBillTemplate) {
        return toAjax(creditBillTemplateService.save(creditBillTemplate));
    }

    /**
     * 修改账单拆分模板
     */
    @SaCheckPermission("credit:billTemplate:edit")
    @Log(title = "账单拆分模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CreditBillTemplate creditBillTemplate) {
        return toAjax(creditBillTemplateService.updateById(creditBillTemplate));
    }

    /**
     * 删除账单拆分模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("credit:billTemplate:remove")
    @Log(title = "账单拆分模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(creditBillTemplateService.removeByIds(Arrays.asList(ids)));
    }
}
